package websocketgulu.demo.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import websocketgulu.demo.pojo.WaveFileReader;

public class GetDataByTime {
    public static int[] getDataByTime(String path, String music_start, String request_start, String request_end) {
        WaveFileReader reader = new WaveFileReader(path);
        int[] all_data = reader.getData()[0];
        long start_index = interval_time(request_start, music_start) * reader.getSampleRate();
        long end_index = interval_time(request_end, music_start) * reader.getSampleRate();
        int[] request_data = new int[(int)(end_index - start_index)];
        System.arraycopy(all_data, (int)start_index, request_data, 0, (int)(end_index - start_index));
        return request_data;
    }

    private static int interval_time(String time1, String time2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = df.parse(time1);
            d2 = df.parse(time2);
            long interval = d1.getTime() - d2.getTime();
            return (int)interval / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println(interval_time("2021-07-22 10:11:30", "2021-07-22 10:12:30"));
    }
}
